import { picUrl, formatTime } from './util.js';
import { activityDetail, getUserH5Openid } from './getData.js'

/**
 * 此页面放体积较大的多场景业务逻辑
 */

function loginOpenid(){
  let than = this;
  return new Promise((resolve, reject,next) => {
    // // 登录
    wx.login({
     success: res => {
       console.log(3333)
       console.log(res)
       // 发送 res.code 到后台换取 openId, sessionKey, unionId
       let data ={
         code:res.code,
        // code : "test"
       }
       getUserH5Openid(data).then(res => {
         console.log('open')
         console.log(res)
         //写入全局
         getApp().globalData.userInfo.openId = res.result.openId
         getApp().globalData.userInfo.unionId = res.result.unionId
         getApp().globalData.userInfo.status = res.result.status

         wx.setStorageSync('openId', res.result.openId)
         wx.setStorageSync('unionId', res.result.unionId)
         wx.setStorageSync('status', res.result.status)
       
         if(res.result.userId){
           //写入缓存
           wx.setStorageSync('userId', res.result.userId)
           resolve(res)
          //  this.getIndexFeedTwo(scene);
         }
       })
     }
   })

  })
}
//局详情转换数据
function getActivityDetail(activityId){
  let than = this;
  let data = {
    activityId:activityId,
    userId:wx.getStorageSync('userId') || '' //微信获取，非必填项
  }
  let cardDatd = {};
  return new Promise((resolve, reject,next) => {
    activityDetail(data).then(res => {
      if(res.success){
      let currentData = res.result;
      // wx.setStorageSync('activityName', currentData.activityName);
      // wx.setStorageSync('userName', currentData.sponsorUser.userName);

      //判断局是否结束
      let currentTime = formatTime(new Date()); //当前时间
       currentTime = currentTime.substring(0,currentTime.length - 9);
       let activityEndTime = currentData.activityDate.replace(/-/g,"/");  //局结束时间
      cardDatd = {
        ...currentData,
        activityAddress:currentData.activityAddress.split(',')[0],
        activityCoverPage:picUrl('activity/img/',currentData.activityCoverPage),
        activityBlurCoverPage:picUrl('activity/img/',currentData.activityBlurCoverPage),
        shareImg:picUrl('',currentData.shareImg),
        activityUserMap:currentData.activityUserMap.participantlist.map((item)=>{
          return {...item, avatarPath:picUrl('',item.avatarPath), userPic:picUrl('',item.userPic)}
        }),
        sponsorUser:{...currentData.sponsorUser,avatarPath:picUrl('',currentData.sponsorUser.avatarPath)},
        activityDate:currentData.activityDate.slice(5).replace(/-/g,"/"),
        activityInvalidation:currentTime > activityEndTime, //局是否失效
      }
      console.log(cardDatd)
      wx.setStorageSync('lookCoverPage', cardDatd.activityCoverPage); //在看的主题图片(局或者活动)，登录流程需要用到
      resolve(cardDatd)
    }else{
      reject(res)
    }
    })
    
  })
}
 
//转化成小程序模板语言
module.exports = {
  loginOpenid,
  getActivityDetail
};

